﻿Imports System.IO
Imports System.Windows.Forms

Public Class ProjectNewFileDialog

    Private Sub ProjectNewFileDialog_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        TextBoxClassname.Focus()

        ' Clear the previous project folders
        ComboBoxProjectFolder.Items.Clear()

        ' Tally up project folders
        Dim curproj = Globals.CurrentProject
        For Each folder In curproj.FileList.ProjectFolders
            If folder.FullName.ToLower().EndsWith("classes") Then
                ComboBoxProjectFolder.Items.Add(
                    folder.FullName.Replace(curproj.DevelopmentFolder, "").Replace("\Classes", "").Replace("\classes", ""))
            End If
        Next

        ComboBoxProjectFolder.SelectedIndex = 0
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Dim curproj = Globals.CurrentProject

        ' Create the new project file
        Dim writer = File.CreateText(curproj.DevelopmentFolder + ComboBoxProjectFolder.Text + "\Classes\" + TextBoxFilename.Text)

        If Not IsNothing(writer) Then
            writer.WriteLine("// Header Comment")
            writer.WriteLine("class " + TextBoxClassname.Text + " extends object")
            writer.Close()

            ' Add file to project view
            Globals.CurrentProject.FileList.RefreshDirectories()

            ' Return the dialog result
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        Else
            MessageBox.Show("Failed to create file: " & TextBoxFilename.Text)
        End If
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub TextBoxClassname_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxClassname.TextChanged
        TextBoxFilename.Text = TextBoxClassname.Text + ".uc"
    End Sub

End Class
